package com.tourex.ui;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.Response.Listener;
import com.tourex.App;
import com.tourex.BaseActivity;
import com.tourex.C;
import com.tourex.C.bean.Api01;
import com.tourex.C.bean.MyLocationInfo;
import com.tourex.C.bean.Tour;
import com.tourex.C.bean.Weather;
import com.tourex.C.bean.WeatherInfo;
import com.tourex.R;
import com.tourex.thirdparty.volley.VolleyUtils;
import com.tourex.thirdparty.zxing.MipcaActivityCapture;
import com.tourex.ui.view.app.MainModuleLayout;
import com.tourex.ui.view.app.MainWeatherLayout;
import com.tourex.ui.view.app.TourProjectInfoParentLayout;
import com.tourex.utils.AnimUtils;
import com.tourex.utils.BaseUtils;
import com.tourex.utils.LogUtils;
import com.tourex.utils.WeatherUtils;

public class MainActivity2 extends BaseActivity {

	private ListView listView;
	private ArrayAdapter<Tour> tourAdapter;

	private LinearLayout layHead;
	private MainWeatherLayout layWeather;
	private MainModuleLayout layModule;

	private View leftMenu, rightMenu;

	private Api01 api01;

	public static void actionStart(Context context) {
		Intent intent = new Intent(context, MainActivity2.class);
		context.startActivity(intent);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_main2);
		super.onCreate(savedInstanceState);

		leftMenu = titleLayout.addLeftDrawableMenu(this, R.drawable.ico_personal, 25, 25, onMenuClickListener);
		rightMenu = titleLayout.addRightDrawableMenu(this, R.drawable.ico_camera, 25, 25, onMenuClickListener);

		leftMenu.setVisibility(View.GONE);

		listView = (ListView) findViewById(R.id.listview);

		layHead = new LinearLayout(this);
		layHead.setOrientation(LinearLayout.VERTICAL);

		layWeather = new MainWeatherLayout(this);

		layModule = new MainModuleLayout(this);

		layHead.addView(layWeather);
		layHead.addView(layModule);
		listView.addHeaderView(layHead);

		// http://222.85.150.217:8099/appServer/gzly/server!loadI01.do?jd=21.235&wd=25.324&city=488
		// App.i().setReceiveLocationListener(onReceiveLocationListener);
		// App.i().initSelfLocationInfo();

		requestApi01(App.i().mli);
		requestApiWeather(App.i().mli);

	}

	class TourAdapter extends ArrayAdapter<Tour> {

		public TourAdapter(Context context, int resourceId, List<Tour> objects) {
			super(context, resourceId, objects);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Tour tour = getItem(position);
			View view;
			ViewHolder viewHolder;
			if (convertView == null) {
				view = LayoutInflater.from(getContext()).inflate(R.layout.list_content_tour_info, null);
				viewHolder = new ViewHolder();
				viewHolder.txTitle = (TextView) view.findViewById(R.id.tx_title);
				// viewHolder.layInfoList = (LinearLayout)
				// view.findViewById(R.id.layout_tour_info_list);
				viewHolder.layTpiParent = (TourProjectInfoParentLayout) view.findViewById(R.id.layout_tour_info_list);
				viewHolder.layTitleMore = view.findViewById(R.id.lay_title_more);

				viewHolder.layTitleMore.setOnClickListener(onLayMoreClickListener);

				view.setTag(viewHolder);

			} else {
				view = convertView;
				viewHolder = (ViewHolder) view.getTag();
			}

			viewHolder.txTitle.setText(tour.title);

			if (tour != null && tour.list != null) {
				viewHolder.layTpiParent.setData(tour.list, tour.type, 100);
			}

			viewHolder.layTitleMore.setTag(tour.type);

			return view;
		}

		class ViewHolder {
			TextView txTitle;
			// LinearLayout layInfoList;
			TourProjectInfoParentLayout layTpiParent;
			View layTitleMore;
		}
	}

	private OnClickListener onLayMoreClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			String type = v.getTag().toString().trim();
			if (C.convention.type_cz.equals(type)) {
				if (App.i().hasLocation) {
					SecondActivity.actionStart(MainActivity2.this, 1, App.i().mli.cityCode);
				} else {
					Toast.makeText(MainActivity2.this, R.string.location_3, Toast.LENGTH_SHORT).show();
				}
			} else {
				GeneralListActivity.actionStart(MainActivity2.this, type, null);
			}
		}
	};

	public void requestApi01(final MyLocationInfo mli) {
		Map<String, String> params = BaseUtils.initMap("wd", mli.lat, "jd", mli.lng, "city", mli.cityCode);
		VolleyUtils.postGson(C.api.api01, Api01.class, params, new Response.Listener<Api01>() {
			@Override
			public void onResponse(Api01 api01) {
				MainActivity2.this.api01 = api01;
				initBasic(api01);
				initTourProjectInfo(api01);
			}
		});
	}

	@SuppressLint("SimpleDateFormat")
	public void requestApiWeather(final MyLocationInfo mli) {
		String areaid = mli.weatherCode;
		String date = new SimpleDateFormat("yyyyMMddHHmm").format(new Date());
		String url = WeatherUtils.loadUrl(areaid, date);
		VolleyUtils.postGson(url, WeatherInfo.class, null, new Listener<WeatherInfo>() {
			@Override
			public void onResponse(WeatherInfo response) {
				Weather w = response.f.f1.get(0);
				LogUtils.logd("weather response:" + w.fc + "-" + w.fd);
				String t = w.fc + " - " + w.fd;
				layWeather.setWeather(t);
			}
		});
	}

	private void initBasic(Api01 api01) {
		startAnimation(leftMenu, AnimUtils.cearteAlpha(0f, 1f));
		layWeather.setData(api01);
	}

	private void initTourProjectInfo(Api01 api01) {
		List<Tour> tourList = api01.xl;

		tourAdapter = new TourAdapter(this, 0, tourList);
		listView.setAdapter(tourAdapter);

		returnToTop();
	}

	private OnClickListener onMenuClickListener = new OnClickListener() {
		@Override
		public void onClick(View view) {
			startAnimation(view, AnimUtils.init().addAlpha(1f, 0.2f, 0, 200, new LinearInterpolator(), Animation.REVERSE).create());

			if (view.getId() == leftMenu.getId()) {
				WapActivity.actionStart(MainActivity2.this, api01.login, C.convention.type_self_info, null, 0d, 0d);
			} else if (view.getId() == rightMenu.getId()) {
				MipcaActivityCapture.actionStart(MainActivity2.this);
			}
		}
	};

	@Override
	protected void onResume() {
		super.onPause();
		layWeather.startSliding();
	};

	@Override
	protected void onPause() {
		super.onPause();
		layWeather.stopSliding();
	};
}
